
import javax.swing.*;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.awt.*;

/**
 * ****************************************************************************
 * $Author: Yegor Chemisov $
 * $Revision: 1.9 $
 * ****************************************************************************
 */

public class ResultsTable implements java.io.Serializable
{
    PlayerResult[] results;

    public ResultsTable()
    {
        results = new PlayerResult[MAPConsts.MAX_RESULTS];
        for (int i = MAPConsts.MAX_RESULTS; i-- > 0; results[i] = new PlayerResult());
    }

    public void addResult(PlayerResult result)
    {
        results[MAPConsts.MAX_RESULTS - 1] = result;

        for (int i = MAPConsts.MAX_RESULTS - 1; i > 0; i--)
        {
            if (results[i].compareTo(results[i - 1]) > 0)
            {
                PlayerResult tmp = results[i];
                results[i] = results[i - 1];
                results[i - 1] = tmp;
            }
        }
    }

    public boolean isResultGood(PlayerResult player)
    {
        return player.compareTo(results[MAPConsts.MAX_RESULTS - 1]) >= 0;
    }

    // ------------------------------------------------------------------------

    public void showResults(JFrame frame)
    {
        String[] columnNames = {
            MAPConsts.RES.getString(GlobalRes.TABLE_CAPTION_PLAYER),
            MAPConsts.RES.getString(GlobalRes.TABLE_CAPTION_MISSES),
            MAPConsts.RES.getString(GlobalRes.TABLE_CAPTION_SECONDS)};

        String[][] data = new String[MAPConsts.MAX_RESULTS][3];

        for (int i = 0; i < MAPConsts.MAX_RESULTS; i++)
        {
           PlayerResult player = results[i];
           if (player.getPlayerName() == null)
           {
               data[i][2] = data[i][1] = data[i][0] =
                       MAPConsts.RES.getString(GlobalRes.TABLE_MSG_NA);
           }
           else
           {
               data[i][0] = player.getPlayerName();
               data[i][1] = String.valueOf(player.getMisses());
               data[i][2] = String.valueOf(player.getSeconds());
           }
        }

        final JTable table = new JTable(data, columnNames);
        table.getColumnModel().getColumn(0).setPreferredWidth(170);
        table.setPreferredScrollableViewportSize(new Dimension(300, 160));
        table.setEnabled(false);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        JOptionPane.showMessageDialog(frame, scrollPane,
                MAPConsts.RES.getString(GlobalRes.MENU_BEST_TIMES),
                JOptionPane.PLAIN_MESSAGE);

    }

    // ------------------------------------------------------------------------

    public static ResultsTable[] loadResults()
    {
        FileInputStream     fileIn  = null;
        ObjectInputStream   in      = null;

        ResultsTable[] results = null;

        try
        {
            fileIn = new FileInputStream(MAPConsts.getResultsFileName());
            in = new ObjectInputStream (fileIn);
            results = (ResultsTable[]) in.readObject();
        }
        catch (java.io.FileNotFoundException e)
        {
            fileIn = null;
            ErrorMessages.error("Can't find results file.");
        }
        catch (java.io.IOException e)
        {
            ErrorMessages.error("Can't read results file. ");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (fileIn != null)
            {
                try { fileIn.close(); } catch (java.io.IOException e) { }
                fileIn = null;
            }

            if (in != null)
            {
            try { in.close(); } catch (java.io.IOException e) { }
            in = null;
            }
        }

        if (results == null)
        {
            results =  new ResultsTable[MAPConsts.NUM_LEVELS];
            for (int i = MAPConsts.NUM_LEVELS; i-- > 0; results[i] = new ResultsTable());
        }

        return results;
    }

    // ------------------------------------------------------------------------

    public static void saveResults(ResultsTable[] results)
    {
        FileOutputStream     fileOut  = null;
        ObjectOutputStream   out      = null;

        try
        {
            fileOut = new FileOutputStream(MAPConsts.getResultsFileName());
            out = new ObjectOutputStream (fileOut);
            out.writeObject(results);
        }
        catch (java.io.FileNotFoundException e)
        {
            fileOut = null;
            ErrorMessages.error("Can't find results file.");
        }
        catch (java.io.IOException e)
        {
            ErrorMessages.error("Can't open results file. ");
        }
        finally
        {
            if (fileOut != null)
            {
                try { fileOut.close(); } catch (java.io.IOException e) { }
                fileOut = null;
            }
        }
    }
}
